/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia;

import Persistencia.exceptions.NonexistentEntityException;
import Persistencia.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.transaction.UserTransaction;
import motoressa.InformeDeDiagnostico;
import motoressa.Vehiculo;
import motoressa.Tecnico;
import motoressa.OrdenDeReparacion;

/**
 *
 * @author HP
 */
public class InformeDeDiagnosticoJpaController implements Serializable {

    public InformeDeDiagnosticoJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public InformeDeDiagnosticoJpaController() {
        emf=Persistence.createEntityManagerFactory("MotoresSAPU");
    }
    

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(InformeDeDiagnostico informeDeDiagnostico) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Vehiculo miVehiculo = informeDeDiagnostico.getMiVehiculo();
            if (miVehiculo != null) {
                miVehiculo = em.getReference(miVehiculo.getClass(), miVehiculo.getNumeroPatente());
                informeDeDiagnostico.setMiVehiculo(miVehiculo);
            }
            Tecnico miTecnico = informeDeDiagnostico.getMiTecnico();
            if (miTecnico != null) {
                miTecnico = em.getReference(miTecnico.getClass(), miTecnico.getDni());
                informeDeDiagnostico.setMiTecnico(miTecnico);
            }
            OrdenDeReparacion miOrdenDeReparacion = informeDeDiagnostico.getMiOrdenDeReparacion();
            if (miOrdenDeReparacion != null) {
                miOrdenDeReparacion = em.getReference(miOrdenDeReparacion.getClass(), miOrdenDeReparacion.getCodigo());
                informeDeDiagnostico.setMiOrdenDeReparacion(miOrdenDeReparacion);
            }
            em.persist(informeDeDiagnostico);
            if (miVehiculo != null) {
                miVehiculo.getMisInformes().add(informeDeDiagnostico);
                miVehiculo = em.merge(miVehiculo);
            }
            if (miTecnico != null) {
                miTecnico.getMisInformes().add(informeDeDiagnostico);
                miTecnico = em.merge(miTecnico);
            }
            if (miOrdenDeReparacion != null) {
                InformeDeDiagnostico oldMiInformeDeDiagnosticoOfMiOrdenDeReparacion = miOrdenDeReparacion.getMiInformeDeDiagnostico();
                if (oldMiInformeDeDiagnosticoOfMiOrdenDeReparacion != null) {
                    oldMiInformeDeDiagnosticoOfMiOrdenDeReparacion.setMiOrdenDeReparacion(null);
                    oldMiInformeDeDiagnosticoOfMiOrdenDeReparacion = em.merge(oldMiInformeDeDiagnosticoOfMiOrdenDeReparacion);
                }
                miOrdenDeReparacion.setMiInformeDeDiagnostico(informeDeDiagnostico);
                miOrdenDeReparacion = em.merge(miOrdenDeReparacion);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findInformeDeDiagnostico(informeDeDiagnostico.getCodigo()) != null) {
                throw new PreexistingEntityException("InformeDeDiagnostico " + informeDeDiagnostico + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(InformeDeDiagnostico informeDeDiagnostico) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            InformeDeDiagnostico persistentInformeDeDiagnostico = em.find(InformeDeDiagnostico.class, informeDeDiagnostico.getCodigo());
            Vehiculo miVehiculoOld = persistentInformeDeDiagnostico.getMiVehiculo();
            Vehiculo miVehiculoNew = informeDeDiagnostico.getMiVehiculo();
            Tecnico miTecnicoOld = persistentInformeDeDiagnostico.getMiTecnico();
            Tecnico miTecnicoNew = informeDeDiagnostico.getMiTecnico();
            OrdenDeReparacion miOrdenDeReparacionOld = persistentInformeDeDiagnostico.getMiOrdenDeReparacion();
            OrdenDeReparacion miOrdenDeReparacionNew = informeDeDiagnostico.getMiOrdenDeReparacion();
            if (miVehiculoNew != null) {
                miVehiculoNew = em.getReference(miVehiculoNew.getClass(), miVehiculoNew.getNumeroPatente());
                informeDeDiagnostico.setMiVehiculo(miVehiculoNew);
            }
            if (miTecnicoNew != null) {
                miTecnicoNew = em.getReference(miTecnicoNew.getClass(), miTecnicoNew.getDni());
                informeDeDiagnostico.setMiTecnico(miTecnicoNew);
            }
            if (miOrdenDeReparacionNew != null) {
                miOrdenDeReparacionNew = em.getReference(miOrdenDeReparacionNew.getClass(), miOrdenDeReparacionNew.getCodigo());
                informeDeDiagnostico.setMiOrdenDeReparacion(miOrdenDeReparacionNew);
            }
            informeDeDiagnostico = em.merge(informeDeDiagnostico);
            if (miVehiculoOld != null && !miVehiculoOld.equals(miVehiculoNew)) {
                miVehiculoOld.getMisInformes().remove(informeDeDiagnostico);
                miVehiculoOld = em.merge(miVehiculoOld);
            }
            if (miVehiculoNew != null && !miVehiculoNew.equals(miVehiculoOld)) {
                miVehiculoNew.getMisInformes().add(informeDeDiagnostico);
                miVehiculoNew = em.merge(miVehiculoNew);
            }
            if (miTecnicoOld != null && !miTecnicoOld.equals(miTecnicoNew)) {
                miTecnicoOld.getMisInformes().remove(informeDeDiagnostico);
                miTecnicoOld = em.merge(miTecnicoOld);
            }
            if (miTecnicoNew != null && !miTecnicoNew.equals(miTecnicoOld)) {
                miTecnicoNew.getMisInformes().add(informeDeDiagnostico);
                miTecnicoNew = em.merge(miTecnicoNew);
            }
            if (miOrdenDeReparacionOld != null && !miOrdenDeReparacionOld.equals(miOrdenDeReparacionNew)) {
                miOrdenDeReparacionOld.setMiInformeDeDiagnostico(null);
                miOrdenDeReparacionOld = em.merge(miOrdenDeReparacionOld);
            }
            if (miOrdenDeReparacionNew != null && !miOrdenDeReparacionNew.equals(miOrdenDeReparacionOld)) {
                InformeDeDiagnostico oldMiInformeDeDiagnosticoOfMiOrdenDeReparacion = miOrdenDeReparacionNew.getMiInformeDeDiagnostico();
                if (oldMiInformeDeDiagnosticoOfMiOrdenDeReparacion != null) {
                    oldMiInformeDeDiagnosticoOfMiOrdenDeReparacion.setMiOrdenDeReparacion(null);
                    oldMiInformeDeDiagnosticoOfMiOrdenDeReparacion = em.merge(oldMiInformeDeDiagnosticoOfMiOrdenDeReparacion);
                }
                miOrdenDeReparacionNew.setMiInformeDeDiagnostico(informeDeDiagnostico);
                miOrdenDeReparacionNew = em.merge(miOrdenDeReparacionNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                int id = informeDeDiagnostico.getCodigo();
                if (findInformeDeDiagnostico(id) == null) {
                    throw new NonexistentEntityException("The informeDeDiagnostico with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(int id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            InformeDeDiagnostico informeDeDiagnostico;
            try {
                informeDeDiagnostico = em.getReference(InformeDeDiagnostico.class, id);
                informeDeDiagnostico.getCodigo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The informeDeDiagnostico with id " + id + " no longer exists.", enfe);
            }
            Vehiculo miVehiculo = informeDeDiagnostico.getMiVehiculo();
            if (miVehiculo != null) {
                miVehiculo.getMisInformes().remove(informeDeDiagnostico);
                miVehiculo = em.merge(miVehiculo);
            }
            Tecnico miTecnico = informeDeDiagnostico.getMiTecnico();
            if (miTecnico != null) {
                miTecnico.getMisInformes().remove(informeDeDiagnostico);
                miTecnico = em.merge(miTecnico);
            }
            OrdenDeReparacion miOrdenDeReparacion = informeDeDiagnostico.getMiOrdenDeReparacion();
            if (miOrdenDeReparacion != null) {
                miOrdenDeReparacion.setMiInformeDeDiagnostico(null);
                miOrdenDeReparacion = em.merge(miOrdenDeReparacion);
            }
            em.remove(informeDeDiagnostico);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<InformeDeDiagnostico> findInformeDeDiagnosticoEntities() {
        return findInformeDeDiagnosticoEntities(true, -1, -1);
    }

    public List<InformeDeDiagnostico> findInformeDeDiagnosticoEntities(int maxResults, int firstResult) {
        return findInformeDeDiagnosticoEntities(false, maxResults, firstResult);
    }

    private List<InformeDeDiagnostico> findInformeDeDiagnosticoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from InformeDeDiagnostico as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public InformeDeDiagnostico findInformeDeDiagnostico(int id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(InformeDeDiagnostico.class, id);
        } finally {
            em.close();
        }
    }

    public int getInformeDeDiagnosticoCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from InformeDeDiagnostico as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
